home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15606 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: nntp.teleport.com!usenet
  2. From: GHouck <hksys@teleport.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Probs with prototype mixes with Functions
  5. Date: 20 Apr 1996 08:15:44 GMT
  6. Organization: systems hk
  7. Message-ID: <4la6fg$7tq@nadine.teleport.com>
  8. References: <dward.1.000DC9CA@melbpc.org.au>
  9. NNTP-Posting-Host: ip-pdx03-45.teleport.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. dward@melbpc.org.au (Darren Ward) wrote:
  16. >when making a function such as:
  17. >    
  18. >float convert(int celcius)
  19. >{
  20. >    float faranheit;
  21. >    faranheit = ((float)celcius*9/5)+32;
  22. >    return faranheit;
  23. >}
  24. >and it is called by the main program as
  25. >    faran = convert(celc);
  26. >where faran is a float and celc is an int.
  27. >
  28. >The problem is that I can't compile it as it keeps giving me errors on return 
  29. >type but as far as I can see the main is waiting for a float and the function 
  30. >should return a float?
  31.  
  32. Darren,
  33.  
  34. If the call to 'convert' precedes the declaration of 'convert' as
  35. a function returning a 'float', does the compiler not assume it 
  36. returns an 'int', since you have no prototype up above stating
  37. otherwise?  I get confused by the behavior, since it seems to me
  38. that the response is usually a run-time error rather than a compile
  39. time error, unless you're using a C++ compiler.  For instance, if
  40. you leave out the 'math.h' header, and use 'sqrt()' without the
  41. header's prototype, there is usually no objection during compile,
  42. but plenty of objection to your results (trying to use the returned
  43. integer bit-pattern as a floating point number).  By the way, I also
  44. think that there is no guarantee that your division of '9/5' will
  45. be what you want, even though you are casting the 'celcius' in
  46. front of it, since they (9 & 5) are both integers, and may or may not
  47. return '1' as a result.
  48.  
  49. Yours, Geoff Houck
  50.  
  51.